package nl.ajonker.railroad.ui.drawcommand;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Stroke;

import nl.ajonker.railroad.dl.Rail;
import nl.ajonker.railroad.ui.Orientation;

public abstract class DrawCommand {
	protected final int dimension = 100;
	protected final Color railColor = Color.BLACK;
	protected final Color trainColor = Color.GREEN;

	protected final Dimension fixedDimension = new Dimension(100, 100);
	protected final Stroke stroke = new BasicStroke(4);
	protected final Stroke mouseOverStroke = new BasicStroke(1);
	protected final Color mouseOverColor = new Color(200, 200, 200);

	protected final Orientation orientation;

	protected Rectangle rectangle;
	protected Rail rail;
	protected Point start;
	protected Point end;
	protected boolean mouseOver = false;

	public DrawCommand() {
		this(Orientation.W_E);
	}

	public DrawCommand(Orientation orientaion) {
		this.orientation = orientaion;
	}

	public Point drawRail(Graphics2D graphics2d, final Point start,
			final Rail rail) {
		this.start = start;
		this.end = calculatePoints();
		this.rail = rail;
		drawRail(graphics2d);
		return end;
	}

	public abstract void drawRail(Graphics2D graphics2d);

	protected abstract Point calculatePoints();

	public synchronized void drawMouseOver(Graphics2D g2d) {
		if (rectangle != null && !mouseOver) {
			g2d.setStroke(mouseOverStroke);
			g2d.setColor(mouseOverColor);
			g2d.draw(rectangle);
			mouseOver = true;
		}
	}

	public synchronized void removeMouseOver(Graphics2D g2d, Color background) {
		if (rectangle != null && mouseOver) {
			g2d.setStroke(mouseOverStroke);
			g2d.setColor(background);
			g2d.draw(rectangle);
			drawRail(g2d);
			mouseOver = false;
		}
	}

	protected static void draw(Graphics g, Point a, Point b) {
		g.drawLine(a.x, a.y, b.x, b.y);
	}

	public boolean contains(Point p) {
		if (rectangle == null)
			return false;
		return rectangle.contains(p);
	}

	public void onClick() {
		// do nothing
	}

}
